﻿using blackhouse.mojoPortalExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.StareKino.Web.Events {
    public partial class BlogDataFilter : System.Web.UI.UserControl {

        #region Fields

        private CinemaPostListModel model;

        private RepeaterData[] years;
        private RepeaterData[] months;

        private string postListUrl = String.Empty;

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (Request.QueryString.AllKeys.Contains("tag"))
                this.Visible = false;
            else {
                this.SetScenario();
                this.PopulateView();
            }
        }

        private void SetScenario() {
            this.model = new CinemaPostListModel();
            if (this.model.GetPostDates().Length > 0) {

                this.postListUrl = Request.RawUrl;
                if (this.postListUrl.Contains('?'))
                    this.postListUrl = this.postListUrl.Substring(0, this.postListUrl.IndexOf('?'));

                this.years = this.GetYears();
                this.months = this.GetMonths();
            }
            else {
                this.years = new RepeaterData[0];
                this.months = new RepeaterData[0];
            }
        }

        private void PopulateView() {
            if (this.years.Length > 0) {
                this.rptYears.DataSource = this.years;
                this.rptYears.DataBind();
            }
            if (this.months.Length > 0) {
                this.rptMonths.DataSource = this.months;
                this.rptMonths.DataBind();
            }
        }

        private RepeaterData[] GetYears() {
            List<RepeaterData> years = new List<RepeaterData>();
            int fromYear = this.model.GetPostDates().Select(dt => dt.Year).Min();
            while (fromYear <= DateTime.Now.Year) {
                RepeaterData data = new RepeaterData();
                data.IsActive = fromYear == this.model.SelectedYear;
                data.Name = fromYear.ToString();
                data.Title = "Rok " + fromYear;
                data.Url = this.postListUrl + "?y=" + fromYear;
                years.Add(data);
                fromYear++;
            }
            return years.ToArray();
        }

        private RepeaterData[] GetMonths() {
            List<RepeaterData> months = new List<RepeaterData>();
            int y = this.model.SelectedYear;
            if (y == 0) y = DateTime.Now.Year;
            int fromMonth = 1;
            while (fromMonth <= 12) {
                RepeaterData data = new RepeaterData();
                data.IsActive = fromMonth == this.model.SelectedMonth;
                data.Name = fromMonth.ToRoman();
                data.Title = new DateTime(2001, fromMonth, 1).ToString("MMMM");
                data.Url = this.postListUrl + "?y=" + y + "&m=" + fromMonth;
                months.Add(data);
                fromMonth++;
            }
            return months.ToArray();
        }

        #endregion

        #region Inner

        protected struct RepeaterData {
            public bool IsActive { get; set; }
            public string Url { get; set; }
            public string Title { get; set; }
            public string Name { get; set; }
        }

        #endregion

    }
}